package com.soft.mainsite.mybatis.mapper;

import com.soft.mainsite.module.web.cms.vo.TCmsContentVo;
import com.soft.mainsite.mybatis.model.TCmsContent;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * TCmsContentMapper
 *
 * @author Jin
 * @create 2017-06-16
 **/
public interface TCmsContentMapper extends Mapper<TCmsContent> {

    /**
     * 根据站点编号删除内容
     *
     * @param siteId
     * @return
     */
    @Delete("delete from site_model_content where site_id = #{siteId}")
    int deleteBySiteId(@Param("siteId") Integer siteId);

    /**
     * 根据条件查询
     *
     * @param content
     * @return
     */
    List<TCmsContent> selectByCondition(TCmsContent content);

    /**
     * 根据条件查询内容
     *
     * @param content
     * @return
     */
    List<Map> selectContentByCondition(TCmsContent content);

    /**
     * 根据表名和栏目id查询
     *
     * @param tableName
     * @param categoryId
     * @param param
     * @return
     */
    List<TCmsContent> selectByTableNameAndMap(@Param("tableName") String tableName, @Param("categoryId") Long categoryId, @Param("param") Map param);

    /**
     * 根据内容id和表名查询
     *
     * @param contentId
     * @param tableName
     * @return
     */
    Map selectByContentIdAndTableName(@Param("contentId") Long contentId,
                                      @Param("tableName") String tableName);

    /**
     * 根据内容id查询
     *
     * @param contentId
     * @return
     */
    Map selectByContentId(@Param("contentId") Long contentId);

    /**
     * 根据站点id和栏目id查询
     *
     * @param siteId
     * @param siteType
     * @param categoryIds
     * @param orderBy
     * @param isHot
     * @param isPic
     * @param isRecommend
     * @param tableName
     * @param whereParam
     * @param valueParam
     * @param sortNum 排序字段
     * @return
     */
    List<Map> selectByContentListBySiteIdAndCategoryIds(@Param("siteId") Integer siteId,
                                                        @Param("siteType") String siteType,
                                                        @Param("categoryIds") Long[] categoryIds,
                                                        @Param("orderBy") Integer orderBy,
                                                        @Param("isHot") Integer isHot,
                                                        @Param("isPic") Integer isPic,
                                                        @Param("isRecommend") Integer isRecommend,
                                                        @Param("tableName") String tableName,
                                                        @Param("whereParam") String whereParam,
                                                        @Param("valueParam") String valueParam,
                                                        @Param("sortNum") Integer sortNum
    );

    /**
     * 根据站点id和栏目id查询
     *
     * @param siteId
     * @param categoryIds
     * @param orderBy
     * @param isHot
     * @param isPic
     * @param isRecommend
     * @return
     */
    List<Map> selectByTopicContentListBySiteIdAndCategoryIds(@Param("siteId") Integer siteId,
                                                             @Param("categoryIds") Long[] categoryIds,
                                                             @Param("orderBy") Integer orderBy,
                                                             @Param("isHot") Integer isHot,
                                                             @Param("isPic") Integer isPic,
                                                             @Param("isRecommend") Integer isRecommend
    );

    /**
     * 通过栏目确定表名范围
     *
     * @param siteId
     * @param categoryIds
     * @param orderBy
     * @param isHot
     * @param isPic
     * @param isRecommend
     * @return
     */
    List<Map> selectTableNameBycategoryIds(@Param("siteId") Integer siteId,
                                           @Param("categoryIds") Long[] categoryIds,
                                           @Param("orderBy") Integer orderBy,
                                           @Param("isHot") Integer isHot,
                                           @Param("isPic") Integer isPic,
                                           @Param("isRecommend") Integer isRecommend);


    /**
     * 查询内容
     *
     * @param siteId
     * @param categoryId
     * @param categoryIds
     * @param orderBy
     * @param siteType
     * @param isRecommend
     * @return
     */
    List<Map> selectRecommendContentList(@Param("siteId") Integer siteId,
                                         @Param("categoryId") Long categoryId,
                                         @Param("categoryIds") String[] categoryIds,
                                         @Param("orderBy") Integer orderBy,
                                         @Param("siteType") String siteType,
                                         @Param("isRecommend") Integer isRecommend);

    /**
     * 获取内容
     *
     * @param tableName
     * @param contentId
     * @return
     */
    Map selectTopicAndModelContentBySiteIdAndCategoryIds(@Param("tableName") String tableName,
                                                         @Param("contentId") Integer contentId
    );


    /**
     * 获取内容和模型字段数据
     *
     * @param tableName
     * @param contentId
     * @return
     */
    Map selectTopicAndModelAndContentBySiteIdAndCategoryIds(@Param("tableName") String tableName,
                                                            @Param("contentId") Integer contentId
    );

    /**
     * 根据内容id和栏目id查询之前内容
     *
     * @param contentId
     * @param categoryId
     * @return
     */
    @Select("SELECT * FROM site_model_content WHERE content_id = (SELECT max(content_id) FROM site_model_content WHERE content_id < #{contentId} AND category_id =#{categoryId}  and status =1)")
    @ResultMap("BaseResultMap")
    TCmsContent selectPrevContentByContentIdAndCategoryId(@Param("contentId") Long contentId, @Param("categoryId") Long categoryId);

    /**
     * 根据内容id和栏目id查询之后内容
     *
     * @param contentId
     * @param categoryId
     * @return
     */
    @Select("SELECT * FROM site_model_content WHERE content_id = (SELECT min(content_id) FROM site_model_content WHERE content_id > #{contentId} AND category_id =#{categoryId}  and status =1)")
    @ResultMap("BaseResultMap")
    TCmsContent selectNextContentByContentIdAndCategoryId(@Param("contentId") Long contentId, @Param("categoryId") Long categoryId);

    /**
     * 根据栏目id查询内容
     *
     * @param categoryId
     * @param siteId
     * @param tableName
     * @return
     */
    @Select("SELECT c.content_id contentId, " +
            "        c.site_id siteId, " +
            "        c.user_id userId, " +
            "        c.category_id categoryId, " +
            "        c.model_id modelId, " +
            "        c.title, " +
            "        c.keywords, " +
            "        c.description, " +
            "        c.top, " +
            "        c.recommend, " +
            "        c.thumb, " +
            "        c.updatedate, " +
            "        c.inputdate, " +
            "        c.status, " +
            "        c.url, " +
            "        c.author, " +
            "        c.view_num viewNum, " +
            "        c.static_filename staticFilename, " +
            "        c.article_content articleContent, " +
            "        c.image_name imageName, " +
            "        c.image_url imageUrl, " +
            "        c.video_name videoName, " +
            "        c.video_url videoUrl, " +
            "        c.site_type siteType, " +
            "        m.*  " +
            "FROM  " +
            "site_model_content c  inner JOIN  t_cms_category  cat  " +
            "ON cat.category_id = c.category_id  " +
            "left join site_model_${tableName} m " +
            "on c.content_id = m.content_id " +
            "WHERE " +
            "cat.category_id=#{categoryId} " +
            "and " +
            "c.status=1 " +
            "order by c.content_id desc")
    @ResultType(Map.class)
    List<Map> selectByCategoyId(@Param("categoryId") Long categoryId, @Param("siteId") Integer siteId, @Param("tableName") String tableName);

    /**
     * 根据栏目父id查询内容
     *
     * @param categoryIds
     * @param siteId
     * @param tableName
     * @return
     */
    @Select("SELECT c.content_id contentId, " +
            "        c.site_id siteId, " +
            "        c.user_id userId, " +
            "        c.category_id categoryId, " +
            "        c.model_id modelId, " +
            "        c.title, " +
            "        c.keywords, " +
            "        c.description, " +
            "        c.top, " +
            "        c.recommend, " +
            "        c.thumb, " +
            "        c.updatedate, " +
            "        c.inputdate, " +
            "        c.status, " +
            "        c.url, " +
            "        c.author, " +
            "        c.view_num viewNum, " +
            "        c.static_filename staticFilename, " +
            "        c.article_content articleContent, " +
            "        c.image_name imageName, " +
            "        c.image_url imageUrl, " +
            "        c.video_name videoName, " +
            "        c.video_url videoUrl, " +
            "        c.site_type siteType, " +
            "        m.*  " +
            "FROM " +
            " site_model_content c " +
            " inner JOIN  t_cms_category  cat " +
            " ON cat.category_id = c.category_id  " +
            "left join site_model_${tableName} m" +
            " on c.content_id = m.content_id " +
            " WHERE c.category_id in (${categoryIds}) " +
            "and c.site_id=#{siteId} and c.status=1" +
            " order by c.content_id desc")
    @ResultType(Map.class)
    List<Map> selectByCategoyParentId(@Param("categoryIds") String categoryIds, @Param("siteId") Integer siteId, @Param("tableName") String tableName);

    /**
     * 查询所有月份数量
     *
     * @return
     */
    @Select(" select " +
            "sum(case month(inputdate) when '1' then 1 else 0 end) as 一月份, " +
            "sum(case month(inputdate) when '2' then 1 else 0 end) as 二月份, " +
            "sum(case month(inputdate) when '3' then 1 else 0 end) as 三月份, " +
            "sum(case month(inputdate) when '4' then 1 else 0 end) as 四月份, " +
            "sum(case month(inputdate) when '5' then 1 else 0 end) as 五月份, " +
            "sum(case month(inputdate) when '6' then 1 else 0 end) as 六月份, " +
            "sum(case month(inputdate) when '7' then 1 else 0 end) as 七月份, " +
            "sum(case month(inputdate) when '8' then 1 else 0 end) as 八月份, " +
            "sum(case month(inputdate) when '9' then 1 else 0 end) as 九月份, " +
            "sum(case month(inputdate) when '10' then 1 else 0 end) as 十月份," +
            "sum(case month(inputdate) when '11' then 1 else 0 end) as 十一月份, " +
            "sum(case month(inputdate) when '12' then 1 else 0 end) as 十二月份  " +
            "from site_model_content " +
            "where year(inputdate)=year(now());")
    @ResultType(Map.class)
    Map selectAllMonthCount();

    /**
     * 更新内容发布状态
     *
     * @param contentId
     * @param status
     * @return
     */
    Integer updateStateByContentId(@Param("contentId") Long contentId, @Param("status") Long status);

    /**
     * 修改发布状态
     *
     * @param ids
     * @return
     */
    Integer updateAudit(@Param("ids") Long[] ids);

    /**
     * 通过栏目id查询内容列表
     *
     * @param categoryId
     * @return
     */
    List<TCmsContentVo> selectContentByCategoryId(@Param("categoryId") Long categoryId);

    /**
     * 通过栏目id查询内容id列表
     *
     * @param categoryId
     * @return
     */
    List<Integer> selectContentIdByCategoryId(@Param("categoryId") Long categoryId);

    /**
     * 通过id查询内容
     *
     * @param contentId
     * @return
     */
    TCmsContentVo selectContentById(@Param("contentId") Long contentId);

    /**
     * 审核不通过
     *
     * @param ids
     * @return 修改几行
     */
    Integer auditFail(@Param("ids") Long[] ids);

    /**
     * 通过栏目和站点类型查询内容列表
     *
     * @param modelId
     * @param siteType
     * @param tableName
     * @return
     */
    List<Map> selectByModelIdAndSiteType(@Param("modelId") Long modelId, @Param("siteType") String siteType, @Param("tableName") String tableName);

    /**
     * 联表查询对应ids的内容列表
     *
     * @param tableName
     * @param contentIds
     * @return
     */
    List<Map> selectByModelIdAndContentIds(@Param("tableName") String tableName, @Param("contentIds") String[] contentIds);

}